﻿using SsrsConverter;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Xml.Linq;
using System.IO;

namespace SsrsConverter.Test
{
    /// <summary>
    /// This is a test class for DataSourceTest and is intended
    /// to contain all DataSourceTest Unit Tests.
    /// </summary>
    [TestClass]
    public class DataSourceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        /// A test for Create
        /// </summary>
        [TestMethod]
        public void ParseTest()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<DataSource xmlns='http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition' xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' Name='AdventureWorks2008R2'>
    <ConnectionProperties>
        <DataProvider>SQL</DataProvider>
        <ConnectString>Data Source=(local);Initial Catalog=AdventureWorks2008R2</ConnectString>
        <IntegratedSecurity>true</IntegratedSecurity>
    </ConnectionProperties>
    <rd:SecurityType>None</rd:SecurityType>
    <rd:DataSourceID>21b92c36-6b7e-46e2-a8ec-989544230ba2</rd:DataSourceID>
</DataSource>";
            XDocument xdoc = XDocument.Parse(xml);

            DataSource ds = DataSource.Parse(xdoc.Root);
            Assert.AreEqual("AdventureWorks2008R2", ds.GetName());
            Assert.AreEqual("SQL", ds.GetDataProvider());
            Assert.AreEqual("Data Source=(local);Initial Catalog=AdventureWorks2008R2", ds.GetConnectString());
            Assert.AreEqual(true, ds.GetIntegratedSecurity());
        }

        [TestMethod]
        public void ParseWithoutIntegratedSecurityTest()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<DataSource xmlns='http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition' xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' Name='AdventureWorks2008R2'>
    <ConnectionProperties>
        <DataProvider>SQL</DataProvider>
        <ConnectString>Data Source=(local);Initial Catalog=AdventureWorks2008R2</ConnectString>
    </ConnectionProperties>
    <rd:SecurityType>None</rd:SecurityType>
    <rd:DataSourceID>21b92c36-6b7e-46e2-a8ec-989544230ba2</rd:DataSourceID>
</DataSource>";
            XDocument xdoc = XDocument.Parse(xml);

            DataSource ds = DataSource.Parse(xdoc.Root);
            Assert.AreEqual("AdventureWorks2008R2", ds.GetName());
            Assert.AreEqual("SQL", ds.GetDataProvider());
            Assert.AreEqual("Data Source=(local);Initial Catalog=AdventureWorks2008R2", ds.GetConnectString());
            Assert.AreEqual(false, ds.GetIntegratedSecurity());
        }

        [TestMethod]
        public void ParseRdsTest()
        {
            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<RptDataSource xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' Name='AdventureWorks2008R2'>
    <ConnectionProperties>
        <Extension>SQL</Extension>
        <ConnectString>Data Source=(local);Initial Catalog=AdventureWorks2008R2</ConnectString>
        <IntegratedSecurity>true</IntegratedSecurity>
    </ConnectionProperties>
    <DataSourceID>146a8adb-807b-48c8-9903-72f357a129c4</DataSourceID>
</RptDataSource>";
            XDocument xdoc = XDocument.Parse(xml);

            DataSource ds = DataSource.Parse(xdoc.Root);
            Assert.AreEqual("AdventureWorks2008R2", ds.GetName());
            Assert.AreEqual("SQL", ds.GetDataProvider());
            Assert.AreEqual("Data Source=(local);Initial Catalog=AdventureWorks2008R2", ds.GetConnectString());
            Assert.AreEqual(true, ds.GetIntegratedSecurity());
        }

        [TestMethod]
        public void DataSourceReferenceTest()
        {
            string rdsFile = "AdventureWorks2008R2.rds";
            string source = string.Format("{0}\\..\\..\\RDLConverter\\samples\\{1}", TestContext.TestRunDirectory, rdsFile);
            string target = string.Format("{0}\\{1}", TestContext.DeploymentDirectory, rdsFile);
            File.Copy(source, target);

            string xml = @"<?xml version='1.0' encoding='utf-8'?>
<DataSource xmlns='http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition' xmlns:rd='http://schemas.microsoft.com/SQLServer/reporting/reportdesigner' Name='AdventureWorks2008R2'>
    <DataSourceReference>AdventureWorks2008R2</DataSourceReference>
    <rd:SecurityType>None</rd:SecurityType>
    <rd:DataSourceID>21b92c36-6b7e-46e2-a8ec-989544230ba2</rd:DataSourceID>
</DataSource>";
            XDocument xdoc = XDocument.Parse(xml);

            DataSource ds = DataSource.Parse(xdoc.Root);
            Assert.AreEqual("AdventureWorks2008R2", ds.GetName());
            Assert.AreEqual("SQL", ds.GetDataProvider());
            Assert.AreEqual("Data Source=(local);Initial Catalog=AdventureWorks2008R2", ds.GetConnectString());
            Assert.AreEqual(true, ds.GetIntegratedSecurity());
        }
    }
}
